home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / util / cli / 0Utils13.lha / 0Utils / Open.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-20  |  3.0 KB  |  142 lines

  1.  
  2. /******************************************************************************
  3.  
  4.     MODULE
  5.     Open.c
  6.  
  7.     DESCRIPTION
  8.     Open a certain _filehandle_
  9.  
  10.     NOTES
  11.     Kickstart 2.0+ required
  12.     compiles w/ SAS/C v6.51
  13.  
  14.     BUGS
  15.     none known
  16.  
  17.     TODO
  18.  
  19.     EXAMPLES
  20.     > set fh `Open T:writetest WRITE`
  21.     > WriteLn $fh This is a test
  22.     > Close $fh
  23.     > cat T:writetest
  24.       This is a test
  25.  
  26.     SEE ALSO
  27.  
  28.     INDEX
  29.  
  30.     HISTORY
  31.     21-02-95 b_noll created
  32.     21-02-95 b_noll added version/format-prefix/offset
  33.     20-03-95 b_noll corrected output
  34.     20-03-95 b_noll added args diagnostics
  35.  
  36.     AUTHOR
  37.     Bernd Noll, Brunnenstrasse 55, D-67661 Kaiserslautern
  38.     b_noll@informatik.uni-kl.de
  39.  
  40. ******************************************************************************/
  41.  
  42. /**************************************
  43.         Includes
  44. **************************************/
  45.  
  46. #ifndef   EXEC_LIBRARIES_H
  47. # include <exec/libraries.h>
  48. #endif /* EXEC_LIBRARIES_H */
  49.  
  50. #ifndef   CLIB_EXEC_PROTOS_H
  51. # include <clib/exec_protos.h>
  52. #endif /* CLIB_EXEC_PROTOS_H */
  53.  
  54. #ifndef   DOS_DOS_H
  55. # include <dos/dos.h>
  56. #endif /* DOS_DOS_H */
  57.  
  58. #ifndef   CLIB_DOS_PROTOS_H
  59. # include <clib/dos_protos.h>
  60. #endif /* CLIB_DOS_PROTOS_H */
  61.  
  62. #include <proto/dos.h>
  63. #include <proto/exec.h>
  64.  
  65. /**************************************
  66.      Defines & Structures
  67. **************************************/
  68.  
  69. #ifndef ABSEXECBASE
  70. #define ABSEXECBASE ((struct ExecBase **)4L)
  71. #endif
  72.  
  73. struct _arg {
  74. /* ******************** USER FORMAT ******************** */
  75. #define FORMAT "FILE/A,READ/S,WRITE/S"
  76.  
  77.     STRPTR file;
  78.     ULONG  read;
  79.     ULONG  write;
  80.  
  81. /* ******************** USER FORMAT ******************** */
  82. }; /* struct _argv */
  83.  
  84. #define MAXPATHLEN 256
  85. #define MAXLINELEN 256
  86.  
  87. #define VERSIONPREFIX    "\0$VER: "
  88. #define VERSIONOFFSET    0
  89. #define FORMATPREFIX    "\0$ARG: "
  90. #define FORMATOFFSET    7
  91.  
  92. /**************************************
  93.         Implementation
  94. **************************************/
  95.  
  96. long _main (void)
  97. {
  98.     const char* version = VERSIONPREFIX "Open 1.1 " __AMIGADATE__  + VERSIONOFFSET;
  99.     long retval = RETURN_FAIL;
  100.     struct ExecBase*SysBase = *ABSEXECBASE;
  101.     struct Library* DOSBase;
  102.  
  103.     if (DOSBase = OpenLibrary (DOSNAME, 37)) {
  104.     struct _arg argv = { 0 };
  105.     APTR   args;
  106.     retval     = RETURN_ERROR;
  107.     if (args = (void*)ReadArgs(FORMATPREFIX FORMAT + FORMATOFFSET, (LONG*)&argv, NULL)) {
  108.  
  109. /* ******************** USER BODY ******************** */
  110.         {
  111.         ULONG mode = MODE_OLDFILE;
  112.         BPTR  file;
  113.  
  114.         if (argv.write)
  115.             mode = argv.read ? MODE_READWRITE : MODE_NEWFILE;
  116.  
  117.         if (file = Open (argv.file, mode)) {
  118.             // MISSING:  REGISTER(file);
  119.             FPrintf (Output(), "%ld\n", file);
  120.             retval = RETURN_OK;
  121.         } else {
  122.             retval = RETURN_ERROR;
  123.         } /* if */
  124.         }
  125. /* ******************** USER BODY ******************** */
  126.         FreeArgs (args);
  127.     } /* if */
  128.  
  129.     if (retval > RETURN_WARN)
  130.         PrintFault(IoErr(), "Open");
  131.  
  132.     CloseLibrary (DOSBase);
  133.     } /* if */
  134.     return (retval);
  135. } /* _main */
  136.  
  137. /******************************************************************************
  138. *****  END Open.c
  139. ******************************************************************************/
  140.  
  141.  
  142.